<template>
  <div class="mainDiv">
    <el-row :gutter="20" class="rowHead">
      <el-col :span="12" class="flex_center">
        <el-button type="success" icon="el-icon-plus" @click="add" v-qx="permission.add" style="margin-right: 5px;">新增员工</el-button>
        <el-select v-model="page.zt" class="ml" @change="getList" v-qx="permission.status">
          <el-option v-for="(s,index) in status" :label="s.txt" :value="s.val" :key="index">
          </el-option>
        </el-select>
      </el-col>
      <el-col :span="12" class="flex_center">
        <el-input placeholder="搜索员工姓名、员工电话" @keyup.enter.native="getList" v-model="page.search">
          <i slot="suffix" class="el-icon-search" style="line-height: 40px;width: 25px;cursor: pointer;" @click="getList"></i>
        </el-input>
        <el-button type="primary" icon="el-icon-h-shuaxin1" style="margin-left: 5px;" @click="getList">刷新列表</el-button>
      </el-col>
    </el-row>
    <el-table :data="list" stripe height="100%">
      <el-table-column prop="id" label="ID" width="80"></el-table-column>
      <el-table-column prop="name" label="员工姓名">
        <template slot-scope='scope'>
          <span @click="openEmployeePage(scope.row.id)" style="cursor:pointer;color:#f50">{{scope.row.name}}</span>
        </template>
      </el-table-column>
      <el-table-column prop="shopname" label="所属分店">
        <template slot-scope="scope">
          {{scope.row.shopname || '直属总部'}}
        </template>
      </el-table-column>
      <el-table-column prop="bumenname" label="所在部门">
        <template slot-scope="scope">
          {{scope.row.bumenname || '直属总部'}}
        </template>
      </el-table-column>
      <el-table-column prop="sex" label="员工性别">
        <template slot-scope="scope">
          <i class="el-icon-h-nv1 color-nv" v-if="!scope.row.sex"></i>
          <i class="el-icon-h-nan1 color-nan" v-else></i>
        </template>
      </el-table-column>
      <el-table-column prop="username" label="员工电话"></el-table-column>
      <el-table-column prop="pname" label="所属职位"></el-table-column>
      <el-table-column prop="zname" label="所属职称"></el-table-column>
      <el-table-column prop="ismanage" label="管理层">
        <template slot-scope="scope">
          <el-tag type="success" v-if="scope.row.ismanage"><i class="el-icon-h-shi"></i></el-tag>
          <el-tag type="danger" v-else><i class="el-icon-h-fou"></i></el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="remarks" label="员工描述" :show-overflow-tooltip="true"></el-table-column>
      <el-table-column prop="addtime" label="添加日期">
        <template slot-scope="scope">
          {{scope.row.addtime|dateTime}}
        </template>
      </el-table-column>
      <el-table-column label="操作" width="120">
        <template slot-scope="scope">
          <el-dropdown size="small" trigger="click" @command="moreMenu" v-if="!scope.row.isdisable">
            <el-button type="primary" size="small">更多操作<i class="el-icon-arrow-down el-icon--right"></i></el-button>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item :command="{row: scope.row, tp: 1}" v-qx="permission.edit">编辑员工</el-dropdown-item>
              <el-dropdown-item :command="{id: scope.row.id, tp: 2}" v-qx="permission.reset">重置密码</el-dropdown-item>
              <el-dropdown-item :command="{id: scope.row.id, st: 1, tp: 3}" v-qx="permission.unstop">员工离职</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
          <el-button-group v-else>
            <el-tooltip content="复职员工" placement="left" :open-delay="800" v-qx="permission.unstop">
              <el-button type="success" icon="el-icon-h-qiyong2" size="mini" @click="stop(scope.row.id,0)"></el-button>
            </el-tooltip>
            <el-tooltip content="彻底删除" placement="left" :open-delay="800">
              <el-button type="danger" icon="el-icon-delete" size="mini" @click="deleteUser(scope.row.id)"></el-button>
            </el-tooltip>
          </el-button-group>
        </template>
      </el-table-column>
    </el-table>
    <div class="block center bg-light">
      <el-pagination @size-change="sizeChange" @current-change="currentChange" :current-page="page.pages" :page-sizes="[15, 30, 50, 100]" :page-size="page.limits" layout="total, sizes, prev, pager, next, jumper" :total="total">
      </el-pagination>
    </div>
  </div>
</template>
<script>
import C from '@/common/common.js'
import Employee from '@/service/organization/employee.js'
import User from '@/service/organization/user.js'

export default {
  name: 'userList',
  data () {
    return {
      list: [],
      total: 0,
      page: {
        pages: 1,
        limits: 15,
        search: '',
        zt: 0
      },
      status: [{
        txt: '正常',
        val: 0
      }, {
        txt: '停职',
        val: 1
      }],
      permission: {
        add: 'userlist_add',
        status: 'userlist_status',
        edit: 'userlist_edit',
        unstop: 'userlist_unstop',
        reset: 'userlist_reset',
        delete: 'userlist_delete'
      }
    }
  },
  methods: {
    moreMenu: function (com) {
      if (com.tp === 1) {
        this.edit(com.row)
      } else if (com.tp === 2) {
        this.resetPassWord(com.id)
      } else if (com.tp === 3) {
        this.stop(com.id, com.st)
      }
    },

    resetPassWord: function (id) {
      C.confirm('重置当前员工帐号密码为当前手机号，是否继续？', async () => {
        let res = await C.ajax('/organization/user/resetpassword', { id: id })
        if (res) C.success(res.data)
      })
    },

    add: async function () {
      let data = await C.openBrowserWindow('/addUser2', {}, {
        width: 400,
        height: 625,
        useSpa: false,
        spaModel: 'organization',
        transparent: true,
        frame: false
      })
      if (data) {
        C.success(data.data)
        this.getList()
      }
    },

    edit: async function (row) {
      let data = await C.openBrowserWindow('/addUser2', { row: row }, {
        width: 400,
        height: 625,
        useSpa: false,
        spaModel: 'organization',
        transparent: true,
        frame: false
      })
      if (data) {
        C.success(data.data)
        this.getList()
      }
    },

    stop: async function (id, st) {
      let that = this
      if (st) {
        C.confirm('停用当前员工, 是否继续?', async () => {
          let res = await C.ajax('/organization/user/unstop', { id: id, st: st }, 'post')
          if (res) {
            C.success(res.data)
            that.getList()
          }
        })
      } else {
        let res = await C.ajax('/organization/user/unstop', { id: id, st: st }, 'post')
        if (res) {
          C.success(res.data)
          that.getList()
        }
      }
    },
    sizeChange: function (val) {
      this.page.limits = val
      this.getList()
    },

    currentChange: function (val) {
      this.page.pages = val
      this.getList()
    },

    getList: async function () {
      let that = this
      let data = await C.ajax('/organization/user/userlist', this.page, 'post')
      if (data) {
        that.list = data.data
        that.total = data.count
      }
    },

    openEmployeePage: function (id) {
      Employee.openEmployeePage(id)
    },
    async deleteUser (id) {
      // 向服务端发送删除员工信息
      C.confirm('警告！ 该操作将彻底删除员工，无法找回，请确定是否继续?', async () => {
        let res = await User.deleteUser(id)
        if (res) {
          C.success(res.data)
          this.getList()
        }
      })
      this.getList()
    }
  },
  mounted: function () {
    this.getList()
  }

}
</script>